package com.hhxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhxy.constants.SystemConstants;
import com.hhxy.handler.exception.CustomException;
import com.hhxy.model.dto.AddRoleDto;
import com.hhxy.model.dto.ChangeRoleStatusDto;
import com.hhxy.model.entity.Role;
import com.hhxy.service.RoleService;
import com.hhxy.utils.response.AppHttpCodeEnum;
import com.hhxy.utils.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author ghp
 * @date 2023/5/4
 * @title
 * @description
 */
@Api(tags = {"后端接口", "角色管理", "系统管理"})
@ApiSupport(author = "ghp", order = 1)
@RestController
@RequestMapping("/system/role")
@Validated
public class BeRoleController {
    @Resource
    private RoleService roleService;


    @GetMapping("/list")
    @ApiOperation(value = "分页条件查询角色信息")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageName", value = "当前页码", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名称"),
            @ApiImplicitParam(name = "status", value = "角色状态 0-正常 1-禁用")
    })
    public ResponseResult list(@NotNull(message = "pageNum不能为空") Integer pageNum,
                               @NotNull(message = "pageSize不能为空") Integer pageSize,
                               String roleName, String status) {
        return roleService.list(pageNum, pageSize, roleName, status);
    }

    @PostMapping
    @ApiOperation(value = "新增角色")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult add(@RequestBody @Valid AddRoleDto addRoleDto) {
        return roleService.add(addRoleDto);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询角色信息")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "角色id")
    public ResponseResult getRoleById(@PathVariable @NotNull(message = "角色id不能为空") Long id) {
        // TODO 这里我就不封装VO了，偷个懒 (●ˇ∀ˇ●)
        Role role = roleService.getById(id);
        return ResponseResult.okResult(role);
    }

    @PutMapping
    @ApiOperation(value = "修改角色信息")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult updateRoleById(@RequestBody @Valid AddRoleDto addRoleDto) {
        return roleService.updateRoleById(addRoleDto);
    }

    @PutMapping("/changeStatus")
    @ApiOperation(value = "修改角色状态")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult changeStatus(@RequestBody @Valid ChangeRoleStatusDto changeRoleStatusDto) {
        boolean flag = roleService.update(new LambdaUpdateWrapper<Role>()
                .eq(Role::getId, changeRoleStatusDto.getRoleId())
                .set(Role::getStatus, changeRoleStatusDto.getStatus()));
        if (!flag) {
            throw new CustomException(AppHttpCodeEnum.SERVICE_UPDATE_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS_UPDATE);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除角色信息")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "ids", value = "角色id不能为空，类型为数组", dataType = "Long[]")
    public ResponseResult deleteRoleByIds(@PathVariable Long[] ids) {
        return roleService.deleteRoleByIds(ids);
    }

    @GetMapping("/listAllRole")
    @ApiOperation(value = "查询所有的角色信息", tags = "用户管理")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult listAllRole() {
        List<Role> roles = roleService.list(new LambdaQueryWrapper<Role>()
                .eq(Role::getStatus, SystemConstants.ROLE_NORMAL));
        return ResponseResult.okResult(roles);
    }



}
